/*
 * A Java class to demonstrate how to load an image from disk with the
 * ImageIO class. Also shows how to display the image by creating an
 * ImageIcon, placing that icon an a JLabel, and placing that label on
 * a JFrame.
 */
package pro.unal.edu.datasource;

/**
 *
 * @author Carlos Eduardo Bernal Cárdenas
 * @version 1.0
 * @created 06-oct-2011
 */
import java.awt.Color;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class ImageDemo {

    public static void main(String[] args) {

        // Display + shell
        Display display = new Display();
        Shell shell = new Shell(display);

        // Load an image
        //ImageData imgData = new ImageData( "your image path" );
        //C:\Users\Carlos\Documents
//        ImageData imgData = new ImageData("c:/Users/Carlos/Documents/DC.jpg");
        ImageData imgData = new ImageData("c:/Users/Carlos/Documents/3x3.jpg");
        Image image = new Image(display, imgData);

        Label imgLabel = new Label(shell, SWT.NONE);
        imgLabel.setImage(image);
        imgLabel.setSize(imgLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT));

        byte[] rgbs = imgData.data;
       
        int x = 1;
        int y = 0;
        int grow = 3;
        if (imgData.depth == 32) {
            grow = 4;
        }
        if (imgData.depth == 24) {
            grow = 3;
        }
        int i = 0;
        Color[][] dataImageColor = new Color[imgData.width][imgData.height];
        for (; i <= imgData.bytesPerLine*imgData.height-1; i += grow) {
//            int a = 127;
            int b = (rgbs[i] + 256) % 256;
            int g = (rgbs[i + 1] + 256) % 256;
            int r = (rgbs[i + 2] + 256) % 256;
            /*if (imgData.depth == 32) {
                a = (rgbs[i + 3] + 128) % 128;
            }*/
            Color c = new Color(r, g, b);
            dataImageColor[(x - 1) % imgData.width][y] = c;
            if ((x % imgData.width) == 0) {
                if (imgData.width % 4 != 0) {
                    i += (4 - (3 * imgData.width % 4));
                }
                y++;
            }
            x++;
        }
        for (int j = 0; j < dataImageColor.length; j++) {
            for (int k = 0; k < dataImageColor[j].length; k++) {
                System.out.print(dataImageColor[k][j]);
            }
            System.out.println("");
        }


        System.out.println("Tipo Img: " + imgData.depth);
        System.out.println("bytes per pixel: " + imgData.bytesPerLine);

        // Run it
        shell.setSize(image.getBounds().width + 26, image.getBounds().width + 50);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

        display.dispose();
        image.dispose();
    }

    public static int unsignedByteToInt(byte b) {
        return (int) b & 0xFF;
    }
}